import 'package:pp_page/pp_page.dart';
import 'package:pp_uikit_demo/page_test/widgets/home_index_page.dart';
import 'package:pp_uikit_demo/page_test/widgets/my_page.dart';
import 'package:pp_uikit_demo/page_test/widgets/search_page.dart';
import 'package:pp_uikit_demo/page_test/widgets/travel_page.dart';

import 'controller.dart';
import 'state.dart';

// ignore: must_be_immutable
class PageTestPage extends PPPage<PageTestController> {
  PageTestPage({Key? key}) : super(key: key);
  final _defaultColor = Colors.grey;
  final _activeColor = Colors.blue;

  final PageController _pageController = PageController(
    initialPage: 0,
  );

  PageTestState get dataState => controller.dataState;

  @override
  PageTestController bindController(BuildContext context) =>
      PageTestController();

  @override
  PPAppBar? buildAppBar(BuildContext context) {
    return PPAppBar(title: "buildAppBar");
  }

  @override
  Widget buildBody(BuildContext context) {
    return PageView(
      controller: _pageController,
      physics: const NeverScrollableScrollPhysics(),
      children: const [
        HomeIndexPage(),
        SearchPage(),
        TravelPage(),
        MyPage(),
      ],
    );
  }

  @override
  Widget buildBottomNavigationBar() {
    return Obx(() => BottomNavigationBar(
          currentIndex: dataState.currentIndex.value,
          onTap: (index) {
            _pageController.jumpToPage(index);
            // setState(() {
            dataState.currentIndex.value = index;
            // });
          },
          type: BottomNavigationBarType.fixed,
          items: [
            _bottomItem('首页', Icons.home, 0),
            _bottomItem('搜索', Icons.search, 1),
            _bottomItem('旅拍', Icons.camera_alt, 2),
            _bottomItem('我的', Icons.account_circle, 3),
          ],
        ));
  }

  _bottomItem(String title, IconData icon, int index) {
    return BottomNavigationBarItem(
      icon: Icon(
        icon,
        color: _defaultColor,
      ),
      activeIcon: Icon(
        icon,
        color: _activeColor,
      ),
      label: title,
    );
  }
}
